﻿namespace Centido.Core.Test
{
    using System;
    using System.Data.Common;
    using System.Linq;
    using System.Threading;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Testing table system columns
    /// </summary>
    [TestClass]
    public class SystemColumns
    {
        /// <summary>
        /// Domain name
        /// </summary>
        public const string DomainName = "FullAdminRights";

        /// <summary>
        /// Domain
        /// </summary>
        private Domain _domain;

        /// <summary>
        /// Table
        /// </summary>
        private Table _table;

        /// <summary>
        /// Test initialize
        /// </summary>
        [TestInitialize]
        public void TestInitialize()
        {
            _domain = Domain.Build(DomainConfiguration.Load(DomainName));

            _domain.DeleteAllTables();

            _table = _domain.AddTable(StringProperty.RandomIdentifier());

            //Immedialtelly renaming the table (for complexity)
            _table.Name = StringProperty.RandomIdentifier();

            _table.GrantAll();
        }

        /// <summary>
        /// Test cleanup
        /// </summary>
        [TestCleanup]
        public void TestCleanUp()
        {
            _domain.DeleteAllTables();
        }

        /// <summary>
        /// Checking is column properties
        /// </summary>
        [TestMethod]
        public void CheckingIdColumnProperties()
        {
            //Id
            const string idColumnName = Db.Tables.UserTable.Col.Id;

            Assert.IsNotNull(_table.GetColumn(idColumnName));
            Assert.AreEqual(_table.GetColumn(idColumnName).ColumnType, ColumnType.Guid);

            var idColumn = _table.GetColumn(idColumnName) as GuidColumn;

            Assert.IsNotNull(idColumn);
            Assert.IsTrue(idColumn.IsNotNull);
            Assert.IsTrue(idColumn.IsSystem);
            Assert.IsNotNull(idColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.Id);
        }

        /// <summary>
        /// Checking owner column properties
        /// </summary>
        [TestMethod]
        public void CheckingOwnerColumnProperties()
        {
            const string ownerColumnName = Db.Tables.UserTable.Col.Owner;

            Assert.IsNotNull(_table.GetColumn(ownerColumnName));
            Assert.AreEqual(_table.GetColumn(ownerColumnName).ColumnType, ColumnType.String);

            var ownerColumn = _table.GetColumn(ownerColumnName) as StringColumn;

            Assert.IsNotNull(ownerColumn);
            Assert.IsTrue(ownerColumn.IsNotNull);
            Assert.AreEqual(ownerColumn.MaxLength, DbRule.IdentifierMaxLength);
            Assert.IsTrue(ownerColumn.IsSystem);
            Assert.IsNotNull(ownerColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.Owner);
        }

        /// <summary>
        /// Checking CreatedBy column properties
        /// </summary>
        [TestMethod]
        public void CheckingCreatedByColumnProperties()
        {
            const string createdByColumnName = Db.Tables.UserTable.Col.CreatedBy;

            Assert.IsNotNull(_table.GetColumn(createdByColumnName));
            Assert.AreEqual(_table.GetColumn(createdByColumnName).ColumnType, ColumnType.String);

            var createdByColumn = _table.GetColumn(createdByColumnName) as StringColumn;

            Assert.IsNotNull(createdByColumn);
            Assert.IsTrue(createdByColumn.IsNotNull);
            Assert.AreEqual(createdByColumn.MaxLength, DbRule.IdentifierMaxLength);
            Assert.IsTrue(createdByColumn.IsSystem);
            Assert.IsNotNull(createdByColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.CreatedBy);
        }

        /// <summary>
        /// Checking CreatedOn column properties
        /// </summary>
        [TestMethod]
        public void CheckingCreatedOnColumnProperties()
        {
            const string createdOnColumnName = Db.Tables.UserTable.Col.CreatedOn;

            Assert.IsNotNull(_table.GetColumn(createdOnColumnName));
            Assert.AreEqual(_table.GetColumn(createdOnColumnName).ColumnType, ColumnType.DateTime);

            var createdOnColumn = _table.GetColumn(createdOnColumnName) as DateTimeColumn;

            Assert.IsNotNull(createdOnColumn);
            Assert.IsTrue(createdOnColumn.IsNotNull);
            Assert.IsTrue(createdOnColumn.IsSystem);
            Assert.IsNotNull(createdOnColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.CreatedOn);
        }

        /// <summary>
        /// Checking UpdatedBy column properties
        /// </summary>
        [TestMethod]
        public void CheckingUpdatedByColumnProperties()
        {
            const string updatedByColumnName = Db.Tables.UserTable.Col.UpdatedBy;

            Assert.IsNotNull(_table.GetColumn(updatedByColumnName));
            Assert.AreEqual(_table.GetColumn(updatedByColumnName).ColumnType, ColumnType.String);

            var updatedByColumn = _table.GetColumn(updatedByColumnName) as StringColumn;

            Assert.IsNotNull(updatedByColumn);
            Assert.IsTrue(updatedByColumn.IsNotNull);
            Assert.AreEqual(updatedByColumn.MaxLength, DbRule.IdentifierMaxLength);
            Assert.IsTrue(updatedByColumn.IsSystem);
            Assert.IsNotNull(updatedByColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.UpdatedBy);
        }

        /// <summary>
        /// Checking UpdatedOn column properties
        /// </summary>
        [TestMethod]
        public void CheckingUpdatedOnColumnProperties()
        {
            const string updatedOnColumnName = Db.Tables.UserTable.Col.UpdatedOn;

            Assert.IsNotNull(_table.GetColumn(updatedOnColumnName));
            Assert.AreEqual(_table.GetColumn(updatedOnColumnName).ColumnType, ColumnType.DateTime);

            var updatedOnColumn = _table.GetColumn(updatedOnColumnName) as DateTimeColumn;

            Assert.IsNotNull(updatedOnColumn);
            Assert.IsTrue(updatedOnColumn.IsNotNull);
            Assert.IsTrue(updatedOnColumn.IsSystem);
            Assert.IsNotNull(updatedOnColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.UpdatedOn);
        }

        /// <summary>
        /// Checking DeletedBy column properties
        /// </summary>
        [TestMethod]
        public void CheckingDeletedByColumnProperties()
        {
            const string deletedByColumnName = Db.Tables.UserTable.Col.DeletedBy;

            Assert.IsNotNull(_table.GetColumn(deletedByColumnName));
            Assert.AreEqual(_table.GetColumn(deletedByColumnName).ColumnType, ColumnType.String);

            var deletedByColumn = _table.GetColumn(deletedByColumnName) as StringColumn;

            Assert.IsNotNull(deletedByColumn);
            Assert.IsFalse(deletedByColumn.IsNotNull);
            Assert.AreEqual(deletedByColumn.MaxLength, DbRule.IdentifierMaxLength);
            Assert.IsTrue(deletedByColumn.IsSystem);
            Assert.IsNotNull(deletedByColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.DeletedBy);
        }

        /// <summary>
        /// Checking DeletedOn column properties
        /// </summary>
        [TestMethod]
        public void CheckingDeletedOnColumnProperties()
        {
            const string deletedOnColumnName = Db.Tables.UserTable.Col.UpdatedOn;

            Assert.IsNotNull(_table.GetColumn(deletedOnColumnName));
            Assert.AreEqual(_table.GetColumn(deletedOnColumnName).ColumnType, ColumnType.DateTime);

            var deletedOnColumn = _table.GetColumn(deletedOnColumnName) as DateTimeColumn;

            Assert.IsNotNull(deletedOnColumn);
            Assert.IsTrue(deletedOnColumn.IsNotNull);
            Assert.IsTrue(deletedOnColumn.IsSystem);
            Assert.IsNotNull(deletedOnColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.DeletedOn);
        }

        /// <summary>
        /// Checking IsActive column properties
        /// </summary>
        [TestMethod]
        public void CheckingIsActiveColumnProperties()
        {
            const string isActiveColumnName = Db.Tables.UserTable.Col.IsActive;

            Assert.IsNotNull(_table.GetColumn(isActiveColumnName));
            Assert.AreEqual(_table.GetColumn(isActiveColumnName).ColumnType, ColumnType.Boolean);

            var isActiveColumn = _table.GetColumn(isActiveColumnName) as BoolColumn;

            Assert.IsNotNull(isActiveColumn);
            Assert.IsTrue(isActiveColumn.IsNotNull);
            Assert.IsTrue(isActiveColumn.IsSystem);
            Assert.IsNotNull(isActiveColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.IsActive);
        }

        /// <summary>
        /// Checking RowVersion column properties
        /// </summary>
        [TestMethod]
        public void CheckingRowVersionColumnProperties()
        {
            const string rowVersionColumnName = Db.Tables.UserTable.Col.RowVersion;

            Assert.IsNotNull(_table.GetColumn(rowVersionColumnName));
            Assert.AreEqual(_table.GetColumn(rowVersionColumnName).ColumnType, ColumnType.Guid);

            var rowVersionColumn = _table.GetColumn(rowVersionColumnName) as GuidColumn;

            Assert.IsNotNull(rowVersionColumn);
            Assert.IsTrue(rowVersionColumn.IsNotNull);
            Assert.IsTrue(rowVersionColumn.IsSystem);
            Assert.IsNotNull(rowVersionColumn.DisplayName);

            Assert.IsNotNull(_table.SystemColumns.RowVersion);
        }

        /// <summary>
        /// Inserting and reading id column
        /// </summary>
        [TestMethod]
        public void InsertingAndReadingIdColumn()
        {
            //Inserting new row
            var id = _table.InsertRow();

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading id column
            var insertedId = _table.SystemColumns.Id.EmptyValue();

            _table.ReadRow(id, insertedId);

            //Id must be not null and equal to inserted value
            Assert.IsNotNull(insertedId.GetGuid());
            Assert.AreEqual(insertedId.GetGuid(), id);
        }

        /// <summary>
        /// Inserting with id and reading id column
        /// </summary>
        [TestMethod]
        public void InsertingWithIdAndReadingIdColumn()
        {
            //Inserting new row specifying id
            var id = Guid.NewGuid();

            _table.InsertRow(id);

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading id column
            var insertedId = _table.SystemColumns.Id.EmptyValue();

            _table.ReadRow(id, insertedId);

            //Id must be not null and equal to inserted value
            Assert.IsNotNull(insertedId.GetGuid());
            Assert.AreEqual(insertedId.GetGuid(), id);
        }

        /// <summary>
        /// Inserting and reading Owner column
        /// </summary>
        [TestMethod]
        public void InsertingAndReadingOwnerColumn()
        {
            //Inserting new row specifying id
            var id = _table.InsertRow();

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading owner column
            var insertedOwner = _table.SystemColumns.Owner.EmptyValue();

            _table.ReadRow(id, insertedOwner);

            //OwnedBy must be not (null or empty)
            Assert.IsNotNull(insertedOwner.GetString());
            Assert.IsFalse(string.IsNullOrWhiteSpace(insertedOwner.GetString()));
        }

        /// <summary>
        /// Inserting with owner and reading owner column
        /// </summary>
        [TestMethod]
        public void InsertingWithOwnerAndReadingOwnerColumn()
        {
            //Inserting new row specifying id and owner
            var id = Guid.NewGuid();
            const string owner = "asdasd";

            _table.InsertRow(id, _table.SystemColumns.Owner.Value(owner));

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading owner column
            var insertedOwner = _table.SystemColumns.Owner.EmptyValue();

            _table.ReadRow(id, insertedOwner);

            //OwnedBy must be not (null or empty)
            Assert.IsNotNull(insertedOwner.GetString());
            Assert.IsFalse(string.IsNullOrWhiteSpace(insertedOwner.GetString()));

            Assert.AreEqual(insertedOwner.GetString(), owner);
        }

        /// <summary>
        /// Inserting null OwnedBy
        /// </summary>
        [TestMethod]
        public void InsertingNullOwnedBy()
        {
            //Inserting row
            var id = Guid.NewGuid();

            _table.InsertRow(id, _table.SystemColumns.Owner.Value(null));

            //Reading Owner, CreatedBy, UpdatedBy columns
            var insertedOwner = _table.SystemColumns.Owner.EmptyValue();
            var insertedCreatedBy = _table.SystemColumns.CreatedBy.EmptyValue();
            var insertedUpdatedBy = _table.SystemColumns.UpdatedBy.EmptyValue();

            _table.ReadRow(id, insertedOwner, insertedCreatedBy, insertedUpdatedBy);

            //Owner, CreatedBy and UpdatedBy columns must be equal
            Assert.IsFalse(string.IsNullOrWhiteSpace(insertedOwner.GetString()));
            Assert.AreEqual(insertedOwner.GetString(), insertedCreatedBy.GetString());
            Assert.AreEqual(insertedUpdatedBy.GetString(), insertedCreatedBy.GetString());
        }

        /// <summary>
        /// Inserting too long OwnedBy value
        /// </summary>
        [TestMethod]
        public void InsertingTooLongOwnedBy()
        {
            //Very long OwnedBy value
            var ownedBy = StringProperty.TooLongIdentifier('A');

            try
            {
                //Inserting a row
                _table.InsertRow(Guid.NewGuid(), _table.SystemColumns.Owner.Value(ownedBy));
            }
            catch (DbException)
            {
                Assert.AreEqual(_table.RowCount, 0);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Inserting and readinng CreatedBy column
        /// </summary>
        [TestMethod]
        public void InsertingAndReadingCreatedByColumn()
        {
            //Inserting new row specifying id
            var id = _table.InsertRow();

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading the column
            var insertedCreatedBy = _table.SystemColumns.CreatedBy.EmptyValue();

            _table.ReadRow(id, insertedCreatedBy);

            //CreatedBy must be not null or epmty
            Assert.IsNotNull(insertedCreatedBy.GetString());
            Assert.IsFalse(string.IsNullOrWhiteSpace(insertedCreatedBy.GetString()));
        }

        /// <summary>
        /// Inserting and reading CreatedOn column
        /// </summary>
        [TestMethod]
        public void InsertingAndReadingCreatedOnColumn()
        {
            //Inserting new row specifying id
            var id = _table.InsertRow();

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading the column
            var insertedCreatedOn = _table.SystemColumns.CreatedOn.EmptyValue();

            _table.ReadRow(id, insertedCreatedOn);

            //CreatedOn must not be null
            Assert.IsNotNull(insertedCreatedOn.GetDateTime());
        }

        /// <summary>
        /// Inserting and reading UpdatedBy column
        /// </summary>
        [TestMethod]
        public void InsertingAndReadingUpdatedByColumn()
        {
            //Inserting new row specifying id
            var id = _table.InsertRow();

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading the column
            var insertedUpdatedBy = _table.SystemColumns.UpdatedBy.EmptyValue();

            _table.ReadRow(id, insertedUpdatedBy);

            //UpdatedBy must be not null or epmty
            Assert.IsNotNull(insertedUpdatedBy.GetString());
            Assert.IsFalse(string.IsNullOrWhiteSpace(insertedUpdatedBy.GetString()));
        }

        /// <summary>
        /// Inserting and reading UpdatedOn column
        /// </summary>
        [TestMethod]
        public void InsertingAndReadingUpdatedOnColumn()
        {
            //Inserting new row specifying id
            var id = _table.InsertRow();

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading the column
            var insertedUpdatedOn = _table.SystemColumns.UpdatedOn.EmptyValue();

            _table.ReadRow(id, insertedUpdatedOn);

            //UpdatedOn must not be null
            Assert.IsNotNull(insertedUpdatedOn.GetDateTime());
        }

        /// <summary>
        /// Inserting and reading RowVersion column
        /// </summary>
        [TestMethod]
        public void InsertingAndReadingRowVersionColumn()
        {
            //Inserting new row specifying id
            var id = _table.InsertRow();

            //Row count must be 1
            Assert.AreEqual(_table.RowCount, 1);

            //Reading the column
            var insertedRowVersion = _table.SystemColumns.RowVersion.EmptyValue();

            _table.ReadRow(id, insertedRowVersion);

            //RowVersion must not be null
            Assert.IsNotNull(insertedRowVersion.GetBinary());
        }

        /// <summary>
        /// Checking Owner, CreatedBy, UpdatedBy equality when inserting
        /// </summary>
        [TestMethod]
        public void CheckingOwnerCreatedByAndUpdatedByEqualityWhenInserting()
        {
            //Inserting a row
            var id = _table.InsertRow();

            //Reading Owner, CreatedBy, UpdatedBy columns
            var insertedOwner = _table.SystemColumns.Owner.EmptyValue();
            var insertedCreatedBy = _table.SystemColumns.CreatedBy.EmptyValue();
            var insertedUpdatedBy = _table.SystemColumns.UpdatedBy.EmptyValue();

            _table.ReadRow(id, insertedOwner, insertedCreatedBy, insertedUpdatedBy);

            //Owner, CreatedBy and UpdatedBy columns must be equal
            Assert.IsFalse(string.IsNullOrWhiteSpace(insertedOwner.GetString()));
            Assert.AreEqual(insertedOwner.GetString(), insertedCreatedBy.GetString());
            Assert.AreEqual(insertedUpdatedBy.GetString(), insertedCreatedBy.GetString());
        }

        /// <summary>
        /// Checking CreatedOn and UpdatedOn equality when inserting
        /// </summary>
        [TestMethod]
        public void CheckingCreatedOnAndUpdatedOnEqualityWhenInserting()
        {
            //Inserting a row
            var id = _table.InsertRow();

            //Reading CreatedOn and UpdatedOn columns
            var insertedCreatedOn = _table.SystemColumns.CreatedOn.EmptyValue();
            var insertedUpdatedOn = _table.SystemColumns.UpdatedOn.EmptyValue();

            _table.ReadRow(id, insertedCreatedOn, insertedUpdatedOn);

            //CreatedOn and UpdatedOn must be equal
            Assert.IsNotNull(insertedCreatedOn.GetDateTime());
            Assert.AreEqual(insertedCreatedOn.GetDateTime(), insertedUpdatedOn.GetDateTime());
        }

        /// <summary>
        /// Inserting rows with duplicate id
        /// </summary>
        [TestMethod]
        public void InsertingRowsWithDuplicateId()
        {
            //Inserting first row
            var id = _table.InsertRow();

            try
            {
                //Inserting second row with the same id
                _table.InsertRow(id);
            }
            catch (DbException)
            {
                Assert.AreEqual(_table.RowCount, 1);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Inserting CreatedBy column (which is read-only)
        /// </summary>
        [TestMethod]
        public void InsertingCreatedByColumn()
        {
            //Read-only column value
            var createdBy = _table.SystemColumns.CreatedBy.Value("my_test_user");

            var id = _table.InsertRow(createdBy);

            //Reading inserted value
            var insertedCreatedBy = _table.SystemColumns.CreatedBy.EmptyValue();

            _table.ReadRow(id, insertedCreatedBy);

            //CreatedBy must not be equal to inserted value
            Assert.IsNotNull(insertedCreatedBy.GetString());
            Assert.AreNotEqual(insertedCreatedBy.GetString(), createdBy.GetString());
        }

        /// <summary>
        /// Inserting CreatedOn column (which is read-only)
        /// </summary>
        [TestMethod]
        public void InsertingCreatedOnColumn()
        {
            //Read-only column value
            var createdOn = _table.SystemColumns.CreatedOn.Value(DateTime.Now.AddMonths(-1));

            var id = _table.InsertRow(createdOn);

            //Reading inserted value
            var insertedCreatedOn = _table.SystemColumns.CreatedOn.EmptyValue();

            _table.ReadRow(id, insertedCreatedOn);

            //CreatedOn must not be equal to inserted value
            Assert.IsNotNull(insertedCreatedOn.GetDateTime());
            Assert.AreNotEqual(insertedCreatedOn.GetDateTime(), createdOn.GetDateTime());
        }

        /// <summary>
        /// Inserting UpdatedBy column
        /// </summary>
        [TestMethod]
        public void InsertingUpdatedByColumn()
        {
            //Read-only column value
            var updatedBy = _table.SystemColumns.UpdatedBy.Value("my_test_user2");

            var id = _table.InsertRow(updatedBy);

            //Reading inserted value
            var insertedUpdatedBy = _table.SystemColumns.UpdatedBy.EmptyValue();

            _table.ReadRow(id, insertedUpdatedBy);

            //UpdatedBy must not be equal to inserted value
            Assert.IsNotNull(insertedUpdatedBy.GetString());
            Assert.AreNotEqual(insertedUpdatedBy.GetString(), updatedBy.GetString());
        }

        /// <summary>
        /// Inserting UpdatedOn column
        /// </summary>
        [TestMethod]
        public void InsertingUpdatedOnColumn()
        {
            //Read-only column value
            var updatedOn = _table.SystemColumns.UpdatedOn.Value(DateTime.Now.AddMonths(-2));

            var id = _table.InsertRow(updatedOn);

            //Reading inserted value
            var insertedUpdatedOn = _table.SystemColumns.UpdatedOn.EmptyValue();

            _table.ReadRow(id, insertedUpdatedOn);

            //UpdatedOn must not be equal to inserted value
            Assert.IsNotNull(insertedUpdatedOn.GetDateTime());
            Assert.AreNotEqual(insertedUpdatedOn.GetDateTime(), updatedOn.GetDateTime());
        }

        /// <summary>
        /// Inserting RowVersion column
        /// </summary>
        [TestMethod]
        public void InsertingRowVersionColumn()
        {
            //Read-only column value
            var rowVersion = _table.SystemColumns.RowVersion.Value(Guid.NewGuid().ToByteArray());

            var id = _table.InsertRow(rowVersion);

            //Reading inserted value
            var insertedRowVersion = _table.SystemColumns.RowVersion.EmptyValue();

            _table.ReadRow(id, insertedRowVersion);

            //UpdatedOn must not be equal to inserted value
            Assert.IsNotNull(insertedRowVersion.GetBinary());
            Assert.AreNotEqual(insertedRowVersion.GetBinary(), rowVersion.GetBinary());
        }

        /// <summary>
        /// Updating OwnedBy column
        /// </summary>
        [TestMethod]
        public void UpdatingOwnedByColumn()
        {
            //Inserting a row
            var id = _table.InsertRow();

            //Updating Owner in the inserted row
            var newOwnedBy = _table.SystemColumns.Owner.Value(StringProperty.RandomIdentifier());

            _table.UpdateRow(id, newOwnedBy);

            //Checking that OwnedBy was updated
            var updatedOwnedBy = _table.SystemColumns.Owner.EmptyValue();

            _table.ReadRow(id, updatedOwnedBy);

            Assert.AreEqual(updatedOwnedBy.GetString(), newOwnedBy.GetString().ToLower());
        }

        /// <summary>
        /// CHecking that Owner column value is lowered when inserting
        /// </summary>
        [TestMethod]
        public void CheckingThatOwnerColumnIsLoweredWhenInserting()
        {
            //Owner
            const string ownerStr = "AAA";

            //Inserting a row
            var id = _table.InsertRow(_table.SystemColumns.Owner.Value(ownerStr));

            Assert.AreEqual(_table.RowCount, 1);

            //Reading the Owner
            var insertedOwner = _table.SystemColumns.Owner.EmptyValue();

            _table.ReadRow(id, insertedOwner);

            Assert.IsNotNull(insertedOwner.GetString());
            Assert.AreEqual(insertedOwner.GetString(), "aaa");
        }

        /// <summary>
        /// CHecking that Owner column value is lowered when updating
        /// </summary>
        [TestMethod]
        public void CheckingThatOwnerColumnIsLoweredWhenUpdating()
        {
            //Inserting a row
            var id = _table.InsertRow();

            Assert.AreEqual(_table.RowCount, 1);

            //Updating the row
            _table.UpdateRow(id, _table.SystemColumns.Owner.Value("AAA"));

            //Reading the Owner
            var insertedOwner = _table.SystemColumns.Owner.EmptyValue();

            _table.ReadRow(id, insertedOwner);

            Assert.IsNotNull(insertedOwner.GetString());
            Assert.AreEqual(insertedOwner.GetString(), "aaa");
        }

        /// <summary>
        /// Checking that UpdatedOn changes after update
        /// </summary>
        [TestMethod]
        public void CheckingThatUpdatedOnChangesAfterUpdate()
        {
            //Inserting a row
            var id = _table.InsertRow();

            Assert.AreEqual(_table.RowCount, 1);

            //Reading UpdatedOn
            var insertedUpdatedOn = _table.SystemColumns.UpdatedOn.EmptyValue();

            _table.ReadRow(id, insertedUpdatedOn);

            Assert.IsNotNull(insertedUpdatedOn.GetDateTime());

            Thread.Sleep(1500);

            //Updating the row
            _table.UpdateRow(id, _table.SystemColumns.Owner.Value("new_owner"));

            //Reading UpdatedOn
            var updatedOnAfterUpdate = _table.SystemColumns.UpdatedOn.EmptyValue();

            _table.ReadRow(id, updatedOnAfterUpdate);

            Assert.IsNotNull(updatedOnAfterUpdate.GetDateTime());
            Assert.AreNotEqual(insertedUpdatedOn.GetDateTime(), updatedOnAfterUpdate.GetDateTime());
        }

        /// <summary>
        /// Checking that RowVersion changes after update
        /// </summary>
        [TestMethod]
        public void CheckingThatRowVersionChangesAfterUpdate()
        {
            //Inserting a row
            var id = _table.InsertRow();

            Assert.AreEqual(_table.RowCount, 1);

            //Reading RowVersion
            var insertedRowVersion = _table.SystemColumns.RowVersion.EmptyValue();

            _table.ReadRow(id, insertedRowVersion);

            Assert.IsNotNull(insertedRowVersion.GetBinary());

            //Updating the row
            _table.UpdateRow(id, _table.SystemColumns.Owner.Value("new_owner"));

            //Reading RowVersion
            var rowVersionAfterUpdate = _table.SystemColumns.RowVersion.EmptyValue();

            _table.ReadRow(id, rowVersionAfterUpdate);

            Assert.IsNotNull(rowVersionAfterUpdate.GetBinary());
            Assert.IsFalse(
                insertedRowVersion.GetBinary().SequenceEqual(rowVersionAfterUpdate.GetBinary()));
        }

        /// <summary>
        /// Trying to delete CreatedBy column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteCreatedByColumn()
        {
            try
            {
                _table.SystemColumns.CreatedBy.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename CreatedBy column
        /// </summary>
        [TestMethod]
        public void TryingToRenameCreatedByColumn()
        {
            try
            {
                _table.SystemColumns.CreatedBy.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set CreatedBy column nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetIsNotNullForCreatedByColumn()
        {
            try
            {
                _table.SystemColumns.CreatedBy.IsNotNull = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set CreatedBy column default value
        /// </summary>
        [TestMethod]
        public void TryingToSetDefaultForCreatedByColumn()
        {
            try
            {
                _table.SystemColumns.CreatedBy.DefaultValue = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }


        /// <summary>
        /// Trying to set CreatedBy column emptiness
        /// </summary>
        [TestMethod]
        public void TryingToSetMaxLengthForCreatedByColumn()
        {
            try
            {
                _table.SystemColumns.CreatedBy.MaxLength = 300;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }


        /// <summary>
        /// Trying to delete UpdatedBy column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteUpdatedByColumn()
        {
            try
            {
                _table.SystemColumns.UpdatedBy.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename UpdatedBy column
        /// </summary>
        [TestMethod]
        public void TryingToRenameUpdatedByColumn()
        {
            try
            {
                _table.SystemColumns.UpdatedBy.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set UpdatedBy column nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetIsNotNullForUpdatedByColumn()
        {
            try
            {
                _table.SystemColumns.UpdatedBy.IsNotNull = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set UpdatedBy column default value
        /// </summary>
        [TestMethod]
        public void TryingToSetDefaultForUpdatedByColumn()
        {
            try
            {
                _table.SystemColumns.UpdatedBy.DefaultValue = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set UpdatedBy column emptiness
        /// </summary>
        [TestMethod]
        public void TryingToSetMaxLengthForUpdatedByColumn()
        {
            try
            {
                _table.SystemColumns.UpdatedBy.MaxLength = 300;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }


        /// <summary>
        /// Trying to delete Owner column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteOwnerColumn()
        {
            try
            {
                _table.SystemColumns.Owner.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename Owner column
        /// </summary>
        [TestMethod]
        public void TryingToRenameOwnerColumn()
        {
            try
            {
                _table.SystemColumns.Owner.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set Owner column nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetIsNotNullForOwnerColumn()
        {
            try
            {
                _table.SystemColumns.Owner.IsNotNull = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set Owner column default value
        /// </summary>
        [TestMethod]
        public void TryingToSetDefaultForOwnerColumn()
        {
            try
            {
                _table.SystemColumns.Owner.DefaultValue = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }


        /// <summary>
        /// Trying to set Owner column emptiness
        /// </summary>
        [TestMethod]
        public void TryingToSetMaxLengthForOwnerColumn()
        {
            try
            {
                _table.SystemColumns.Owner.MaxLength = 300;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to delete DeletedBy column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteDeletedByColumn()
        {
            try
            {
                _table.SystemColumns.DeletedBy.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename DeletedBy column
        /// </summary>
        [TestMethod]
        public void TryingToRenameDeletedByColumn()
        {
            try
            {
                _table.SystemColumns.DeletedBy.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set DeletedBy column nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetIsNotNullForDeletedByColumn()
        {
            try
            {
                _table.SystemColumns.DeletedBy.IsNotNull = true;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set DeletedBy column default value
        /// </summary>
        [TestMethod]
        public void TryingToSetDefaultForDeletedByColumn()
        {
            try
            {
                _table.SystemColumns.DeletedBy.DefaultValue = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }


        /// <summary>
        /// Trying to set DeletedBy column emptiness
        /// </summary>
        [TestMethod]
        public void TryingToSetMaxLengthForDeletedByColumn()
        {
            try
            {
                _table.SystemColumns.DeletedBy.MaxLength = 300;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }


        /// <summary>
        /// Trying to delete CreatedOn column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteCreatedOnColumn()
        {
            try
            {
                _table.SystemColumns.CreatedOn.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename CreatedOn column
        /// </summary>
        [TestMethod]
        public void TryingToRenameCreatedOnColumn()
        {
            try
            {
                _table.SystemColumns.CreatedOn.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set CreatedOn nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetCreatedOnNullability()
        {
            try
            {
                _table.SystemColumns.CreatedOn.IsNotNull = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set CreatedOn default value
        /// </summary>
        [TestMethod]
        public void TryingToSetCreatedOnDefaultValue()
        {
            try
            {
                _table.SystemColumns.CreatedOn.DefaultValue = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set CreatedOn minimum
        /// </summary>
        [TestMethod]
        public void TryingToSetCreatedOnMinimum()
        {
            try
            {
                _table.SystemColumns.CreatedOn.Minimum = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set CreatedOn maximum
        /// </summary>
        [TestMethod]
        public void TryingToSetCreatedOnMaximum()
        {
            try
            {
                _table.SystemColumns.CreatedOn.Maximum = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to delete UpdatedOn column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteUpdatedOnColumn()
        {
            try
            {
                _table.SystemColumns.UpdatedOn.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename UpdatedOn column
        /// </summary>
        [TestMethod]
        public void TryingToRenameUpdatedOnColumn()
        {
            try
            {
                _table.SystemColumns.UpdatedOn.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set UpdatedOn nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetUpdatedOnNullability()
        {
            try
            {
                _table.SystemColumns.UpdatedOn.IsNotNull = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set UpdatedOn default value
        /// </summary>
        [TestMethod]
        public void TryingToSetUpdatedOnDefaultValue()
        {
            try
            {
                _table.SystemColumns.UpdatedOn.DefaultValue = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set UpdatedOn minimum
        /// </summary>
        [TestMethod]
        public void TryingToSetUpdatedOnMinimum()
        {
            try
            {
                _table.SystemColumns.UpdatedOn.Minimum = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set UpdatedOn maximum
        /// </summary>
        [TestMethod]
        public void TryingToSetUpdatedOnMaximum()
        {
            try
            {
                _table.SystemColumns.UpdatedOn.Maximum = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to delete DeletedOn column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteDeletedOnColumn()
        {
            try
            {
                _table.SystemColumns.DeletedOn.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename DeletedOn column
        /// </summary>
        [TestMethod]
        public void TryingToRenameDeletedOnColumn()
        {
            try
            {
                _table.SystemColumns.DeletedOn.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set DeletedOn nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetDeletedOnNullability()
        {
            try
            {
                _table.SystemColumns.DeletedOn.IsNotNull = true;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set DeletedOn default value
        /// </summary>
        [TestMethod]
        public void TryingToSetDeletedOnDefaultValue()
        {
            try
            {
                _table.SystemColumns.DeletedOn.DefaultValue = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set DeletedOn minimum
        /// </summary>
        [TestMethod]
        public void TryingToSetDeletedOnMinimum()
        {
            try
            {
                _table.SystemColumns.DeletedOn.Minimum = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set DeletedOn maximum
        /// </summary>
        [TestMethod]
        public void TryingToSetDeletedOnMaximum()
        {
            try
            {
                _table.SystemColumns.DeletedOn.Maximum = DateTime.Now;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to delete RowVersion column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteRowVersionColumn()
        {
            try
            {
                _table.SystemColumns.RowVersion.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename RowVersion column
        /// </summary>
        [TestMethod]
        public void TryingToRenameRowVersionColumn()
        {
            try
            {
                _table.SystemColumns.RowVersion.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set RowVersion column nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetRowVersionColumnNullability()
        {
            try
            {
                _table.SystemColumns.RowVersion.IsNotNull = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set RowVersion column default value
        /// </summary>
        [TestMethod]
        public void TryingToSetRowVersionColumnDefaultValue()
        {
            try
            {
                _table.SystemColumns.RowVersion.DefaultValue = Guid.NewGuid();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to delete IsActive column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteIsActiveColumn()
        {
            try
            {
                _table.SystemColumns.IsActive.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename IsActive column
        /// </summary>
        [TestMethod]
        public void TryingToRenameIsActiveColumn()
        {
            try
            {
                _table.SystemColumns.IsActive.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set IsActive column nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetIsActiveColumnNullability()
        {
            try
            {
                _table.SystemColumns.IsActive.IsNotNull = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set IsActive default value
        /// </summary>
        [TestMethod]
        public void TryingToSetIsActiveDefault()
        {
            try
            {
                _table.SystemColumns.IsActive.DefaultValue = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to delete Id column
        /// </summary>
        [TestMethod]
        public void TryingToDeleteIdColumn()
        {
            try
            {
                _table.SystemColumns.Id.Delete();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.DeletingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to rename Id column
        /// </summary>
        [TestMethod]
        public void TryingToRenameIdColumn()
        {
            try
            {
                _table.SystemColumns.Id.Name = "new_name";
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.RenamingSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set Id column nullability
        /// </summary>
        [TestMethod]
        public void TryingToSetIdColumnNullability()
        {
            try
            {
                _table.SystemColumns.Id.IsNotNull = false;
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Trying to set Id column default value
        /// </summary>
        [TestMethod]
        public void TryingToSetIdColumnDefaultValue()
        {
            try
            {
                _table.SystemColumns.Id.DefaultValue = Guid.NewGuid();
            }
            catch (DbException e)
            {
                Assert.AreEqual(e.GetErrorCode(), DbErrorCode.ChangingSystemPropertyOfSystemColumn);
                return;
            }

            Assert.Fail();
        }

        /// <summary>
        /// Changing minor properties of system columns
        /// </summary>
        [TestMethod]
        public void ChangingMinorPropertiesOfSystemColumns()
        {
            _table.SystemColumns.Id.DisplayName = "Display name";
            _table.SystemColumns.Id.Description = "Description";

            Assert.AreEqual(_table.SystemColumns.Id.DisplayName, "Display name");
            Assert.AreEqual(_table.SystemColumns.Id.Description, "Description");
        }

        /// <summary>
        /// Trying to update id column
        /// </summary>
        [TestMethod]
        public void TryingToUpdateIdColumn()
        {
            Assert.AreEqual(0, _table.RowCount);
            
            var id = _table.InsertRow();

            _table.SystemColumns.Id.Update(id, Guid.NewGuid().ToByteArray());

            _table.SystemColumns.Id.ReadAll(
                idValue => Assert.AreEqual(id, idValue.GetGuid()));
        }


        /// <summary>
        /// Trying to update CreatedOn column
        /// </summary>
        [TestMethod]
        public void TryingToUpdateCreatedOnColumn()
        {
            Assert.AreEqual(0, _table.RowCount);

            var id = _table.InsertRow();

            var createdOn = _table.SystemColumns.CreatedOn.Read(id).GetDateTime();

            _table.SystemColumns.CreatedOn.Update(id, DateTime.Now.AddYears(2));

            Assert.AreEqual(createdOn, _table.SystemColumns.CreatedOn.Read(id).GetDateTime());
        }


        /// <summary>
        /// Trying to update CreatedBy column
        /// </summary>
        [TestMethod]
        public void TryingToUpdateCreatedByColumn()
        {
            Assert.AreEqual(0, _table.RowCount);

            var id = _table.InsertRow();

            var createdBy = _table.SystemColumns.CreatedBy.Read(id).GetString();

            _table.SystemColumns.CreatedBy.Update(id, StringProperty.RandomIdentifier());

            Assert.AreEqual(createdBy, _table.SystemColumns.CreatedBy.Read(id).GetString());
        }

        /// <summary>
        /// Trying to update UpdatedBy column
        /// </summary>
        [TestMethod]
        public void TryingToUpdateUpdatedByColumn()
        {
            Assert.AreEqual(0, _table.RowCount);

            var id = _table.InsertRow();

            var updatedBy = _table.SystemColumns.UpdatedBy.Read(id).GetString();

            _table.SystemColumns.UpdatedBy.Update(id, StringProperty.RandomIdentifier());

            Assert.AreEqual(updatedBy, _table.SystemColumns.UpdatedBy.Read(id).GetString());
        }


        /// <summary>
        /// Trying to update UpdatedOn column
        /// </summary>
        [TestMethod]
        public void TryingToUpdateUpdatedOnColumn()
        {
            Assert.AreEqual(0, _table.RowCount);

            var id = _table.InsertRow();

            var updatedOn = _table.SystemColumns.UpdatedOn.Read(id).GetDateTime();

            _table.SystemColumns.UpdatedOn.Update(id, DateTime.Now.AddYears(2));

            Assert.IsTrue((updatedOn.GetValueOrDefault() - _table.SystemColumns.UpdatedOn.Read(id).
                GetDateTime().GetValueOrDefault()).Seconds < 2);
        }


    }
}